package ui;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;    //JTabbedPane  里的事件需要引入此包

import java.awt.event.*;

import service.*;
import model.*;
import dao.*;
import util.*;

public class AdminUI extends JFrame {

    
    private UserDao userDao=UserDao.getInstance();
    private RoomDao roomDao=RoomDao.getInstance();
    private RecordDao recordDao=RecordDao.getinstance();
    private RoomService roomService=RoomService.getInstance();
    private UserService userService=UserService.getInstance();
 

                                                                                 //!!!!!
    private Toolkit toolkit = Toolkit.getDefaultToolkit();                       
    private Dimension sc = toolkit.getScreenSize();
    private JLabel card0 = new JLabel();
    private JPanel card1 = new JPanel();
    private JLabel card2 = new JLabel();
    private JLabel card3 = new JLabel();
    private JPanel card4 = new JPanel();
    private JLabel card5 = new JLabel();
    private JPanel card6 =new JPanel();

    
    private JLabel uid1 = new JLabel("用户账号");
    private JLabel name1 = new JLabel("姓名");
    private JLabel pass1 = new JLabel("密码");
    private JTextField textUid1 = new JTextField();
    private JTextField textName1  =new JTextField();  
    private JTextField textPass1  =new JTextField();        
    private JButton bQueryU = new JButton("查 找");
    private JLabel tip = new JLabel("您可以做如下操作：");
    private JButton bDelU = new JButton("删除该用户");
    private JPasswordField textNewPass = new JPasswordField();
    private JButton bNewPass = new JButton("修改其密码");

    private JLabel uid2=new JLabel("账号");
	private JLabel pass2=new JLabel("密码");
	private JLabel name2=new JLabel("姓名");
	private JLabel sex=new JLabel("性别");
	private JLabel age=new JLabel("年龄");
	private JLabel nationality=new JLabel("国籍");
	private JLabel fromCountry=new JLabel("来源国");
	private JLabel id=new JLabel("证件号");
	private JTextField tuid=new JTextField();
	private JPasswordField tpass=new JPasswordField();
	private JTextField tname=new JTextField();
	private JTextField tsex=new JTextField();
	private JTextField tage=new JTextField();
	private JTextField tnationality=new JTextField();
	private JTextField tfromCountry=new JTextField();
	private JTextField tid=new JTextField();
    private JButton bConfirm=new JButton("确认");

	
	
    private JLabel type = new JLabel("房间类型");
    private JButton bQueryR = new JButton("查 找");
    private JTextField textType = new JTextField();
    private JLabel rid = new JLabel("房间序号");
    private JLabel address = new JLabel("房间位置");
    private JLabel count = new JLabel("剩余数目");
    private JLabel price = new JLabel("价格");
    private JTextField textRid = new JTextField();
    private JTextField textAddress = new JTextField();
    private JTextField textCount = new JTextField();
    private JTextField textPrice = new JTextField();
    private JButton bIn = new JButton("办理入住");
    private JButton bReturn = new JButton("办理退房");
    
   private String[] userTableHead = {"账号" ,"密码", "姓名","性别","年龄","国籍","来源国","证件号"};      //JTable的表头
   private String[] roomTableHead = {"房间序号", "类型","所在位置","剩余数量","价格"};
   private String[] recordTableHead= {"序号","姓名","房间序号","入住时间","退房时间"};
   
    private Font font = new Font("楷体", 0, 20);
    private Font font1 = new Font("楷体", 0, 16);

    private JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.LEFT);          //选项卡式窗格
    private JButton bRefresh = new JButton("刷新");
    private JButton bExit = new JButton("退出");                    
    private JMenuBar menuBar = new JMenuBar();

    public AdminUI() {
    	setBounds((sc.width - 900) / 2, (sc.height - 500) / 2, 900, 500);  //同时设置位置与大小
	     setResizable(false);
    	tabbedPane.addTab("欢迎管理员", card0);// 后面将card0设为不可用
        tabbedPane.addTab("1.所有用户信息", card1);
        tabbedPane.addTab("2.用户管理", card2);
        tabbedPane.addTab("3.添加用户", card3);
        tabbedPane.addTab("4.查看所有房间", card4);
        tabbedPane.addTab("5.业务办理", card5);
        tabbedPane.addTab("6.查看所有记录", card6);
        tabbedPane.setSelectedIndex(0);     //默认第一个选项卡
        tabbedPane.setEnabledAt(0, false);// 将card0设为不可用
        tabbedPane.setFont(font);// 左栏字体，字号
        
       
        tabbedPane.addChangeListener(new ChangeListener() {
        	
        	public void stateChanged(ChangeEvent e) {
                int selectIndex = tabbedPane.getSelectedIndex();
                if (selectIndex == 1) {
                    initCard1();
                } else if (selectIndex ==2) {
                    initCard2();
                } else if (selectIndex == 3) {
                    initCard3();
                } else if (selectIndex == 4) {
                    initCard4();
                } else if (selectIndex == 5) {
                    initCard5();
                } else if(selectIndex == 6) {
                	initCard6();                }
            }
        });
        
        setJMenuBar(menuBar);   
        menuBar.add(bRefresh);
        menuBar.add(bExit);  
        add(tabbedPane);
        
        
        
        //添加事件监听器
        
        bRefresh.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		AdminUI.adminShow();
        		dispose();
        	}
        });
        bExit.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		setVisible(false);
        		Login.loginShow();
        	}
        });
        bQueryU.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		if(textUid1.getText().equals("")) {
        			ShowMessage.show("请先输入用户账号！");
        		}else {
        		int i=0;
        		try {
        			i=Integer.parseInt(textUid1.getText());
        		}catch(Exception e1) {
        			e1.printStackTrace();
        			ShowMessage.show("请输入有效账号！");
        			}if(userDao.queryUser(i, null)==null) {
        			ShowMessage.show("未找到该用户！\n请检查用户账号是否输入正确！");
        			}else {
        				textPass1.setText(userDao.queryUser(i,null).getPassword());
        				textName1.setText(userDao.queryUser(i, null).getName());
        			}
        		}
        	}
        });
        bDelU.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        	if(textPass1.getText().equals("")) {
        		ShowMessage.show("请先查找一个用户！");
        	}else {
        		int i=0;
        		try {
        			i=Integer.parseInt(textUid1.getText());
        		}catch(Exception e1) {
        			e1.printStackTrace();
        			ShowMessage.show("用户账号格式错误！\n请输入纯数字账号！");
        			}
        		if(userDao.delUser(i)) {
        			ShowMessage.show("删除用户成功！");
        		}else {
        			ShowMessage.show("删除用户时发生错误");
        		}
        	}
        	}
        });
        bNewPass.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		if(bNewPass.getText().equals("")) {
        			ShowMessage.show("新密码为空！");
        		}else {
        			int u=Integer.parseInt(textUid1.getText());
        			String np=new String(textNewPass.getPassword());

        			if(userDao.updUserPassword(u,np)) {
        				ShowMessage.show("密码修改成功！");
        			}else {
        				ShowMessage.show("密码修改失败！");
        			}
        		}
        	}
        });
        bConfirm.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {		 
				 String pass=new String(tpass.getPassword());	 
				 String name=tname.getText();
				 String sex=tsex.getText();
				 String n=tnationality.getText();
				 String f=tfromCountry.getText();
				 String id=tid.getText();
				 
				 
				 if(tuid.getText().equals("")||pass.equals("")||pass2.equals("")||name.equals("")
						 ||sex.equals("")||n.equals("")||f.equals("")||tage.getText().equals("")||id.equals("")) {
					 ShowMessage.show("有项目未填！\n请填写完整！");
				 }else {
				 int uid=0;
				  try {
                   // String型id转int型id，不可转（非数字字符）时会抛异常
					  uid=Integer.parseInt(tuid.getText()); 
               } catch (Exception e1) {
                   e1.printStackTrace();
                   ShowMessage.show("账号格式错误！\n请输入纯数字账号！");
               }
				  
				  int age=0;
				  try {
	                    
					   age=Integer.parseInt(tage.getText()); 
	                } catch (Exception e2) {
	                    e2.printStackTrace();
	                    ShowMessage.show("输入年龄格式错误！\n请输入纯数字！");
	                }
				  	userService.addUser(uid, pass, name, sex, age, n, f, id);
					 
				 }
        	}
        });
         bQueryR.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		String t=textType.getText();
        		if(t.equals("")) {
        			ShowMessage.show("请先输入房间类型！");
        		}else {
        			Room r=roomDao.queryRoom(t);
        			if(r.getRid()==0) {                            //这里不能用if(r==null)，因为queryRoom决定了其永远不可能为空
        				ShowMessage.show("请输入有效房间名称！");
        			}else {
        			String s=Integer.toString(r.getRid());
        			String c=Integer.toString(r.getCount()-r.getHasRented());//求得剩余数并转化为字符型
        			String p=Integer.toString(r.getPrice());
        			textRid.setText(s);
        			textAddress.setText(r.getAddress());
        			textCount.setText(c);
        			textPrice.setText(p);
        			}
        			
        		}
        	}
        });
         bIn.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		if((textRid.getText().equals(""))){
        			ShowMessage.show("请先查找一个房间！");
        		}else {
        			String s1=JOptionPane.showInputDialog(null, "请输入一个账号:");//这里要先判断是否为空，否则输入对话框点取消后会报错
        			if(s1==null) {
        				
        			}else {
        				int uid=Integer.parseInt(s1);
        			if(userDao.queryUser(uid, null)==null) {
        				ShowMessage.show("请输入有效账号！");
        			}else {
        			String rentTime=JOptionPane.showInputDialog("请输入办理入住时间:");
        				if(rentTime==null) {
        				
        				}else {
        					int j=Integer.parseInt(textCount.getText());//判断是否可借
        						roomService.rentRoom(uid,Integer.parseInt( textRid.getText()), rentTime, j);
        				}
        			}
        		}
        			
        		}
        		
        	}
        });
        bReturn.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		if((textRid.getText().equals(""))){
        			ShowMessage.show("请先查找一个房间！");
        		}else {
        			String s2=JOptionPane.showInputDialog(null, "请输入一个账号:");
        			if(s2==null) {
        				
        			}else {
        				int uid=Integer.parseInt(s2);
        				if(userDao.queryUser(uid, null)==null) {
        				ShowMessage.show("请输入有效账号！");
        				}else {
        					String returnTime=JOptionPane.showInputDialog("请输入办理退房时间:");
        					if(returnTime==null) {
        				
        					}else {
        						roomService.returnRoom(uid,Integer.parseInt( textRid.getText()), returnTime);
        					}
        				}
        			}
        		}
        	}
        });
        
        
        

    }
  
    private void initCard1() {
        
        JTable userTable = new JTable(userDao.queryAllUser(), userTableHead);              //JTable用于显示和编辑单元的规则的二维表。
        userTable.setFont(font);
        userTable.setPreferredSize(new Dimension(720, 450));  /*setSize是设定的固定大小，而setPreferredSize仅仅是设置最好的大小，
                                                                                                                    这个不一定与实际显示出来的控件大小一致（根据界面整体的变化而变化）*/
        userTable.getTableHeader().setPreferredSize(new Dimension(0, 30));
        userTable.getTableHeader().setFont(font1);
        userTable.setRowHeight(30);   //设置行高
        userTable.setEnabled(false); // 设置为不开操作
        JScrollPane scroll = new JScrollPane(userTable);
        scroll.setPreferredSize(new Dimension(720, 430));
        card1.add(scroll);
        card1.setFont(font);
    }
    
    private void initCard2() {
        uid1.setBounds(150, 50, 90, 30);
        textUid1.setBounds(240, 50, 200, 30);
        bQueryU.setBounds(450, 50, 80, 30);
        name1.setBounds(150,150,90,30);
        textName1.setBounds(240,150,200,30);
        pass1.setBounds(150,220,90,30);
        textPass1.setBounds(240, 220, 200, 30);
        tip.setBounds(170, 350, 200, 30);
        bDelU.setBounds(370, 350, 120, 30);
        bNewPass.setBounds(370, 400, 120, 30);
        textNewPass.setBounds(170, 400, 170, 30);
        
        card2.setFont(font);
        uid1.setFont(font);
        textUid1.setFont(font);
        bDelU.setFont(font1);
        name1.setFont(font);
        bNewPass.setFont(font1);
        bQueryU.setFont(font1);
        pass1.setFont(font);
        textNewPass.setEchoChar('*');
        
        textName1.setEditable(false);
        textPass1.setEditable(false);
        
        card2.add(textUid1);
        card2.add(uid1);
        card2.add(bQueryU);
        card2.add(name1);
        card2.add(textName1);
        card2.add(pass1);
        card2.add(tip);
        card2.add(bDelU);
        card2.add(bNewPass);
        card2.add(textNewPass);
        card2.add(textPass1);
    }
    private void initCard3() {
       uid2.setBounds(150, 40, 80, 20);
        tuid.setBounds(230, 40, 200, 20);
        pass2.setBounds(150, 70, 80, 20);
        tpass.setBounds(230, 70, 200, 20);
        name2.setBounds(150, 100, 80, 20);
        tname.setBounds(230, 100, 200, 20);
        sex.setBounds(150, 130, 80, 20);
        tsex.setBounds(230, 130, 200, 20);
        age.setBounds(150, 160, 80, 20);
        tage.setBounds(230, 160, 200, 20);
        nationality.setBounds(150, 190, 80, 20);
        tnationality.setBounds(230, 190, 200, 20);
        fromCountry.setBounds(150, 220, 80, 20);
        tfromCountry.setBounds(230, 220, 200, 20);
        id.setBounds(150, 250, 80, 20);
        tid.setBounds(230, 250, 200, 20);
        bConfirm.setBounds(250, 350, 90, 20);

        tpass.setEchoChar('*');// 可以将密码显示为* ；默认为· 但默认又对其设置了字体时会乱码
        bConfirm.setFont(font);
        
        card3.add(uid2);
        card3.add(tuid);
        card3.add(pass2);
        card3.add(name2);
        card3.add(sex);
        card3.add(age);
        card3.add(nationality);
        card3.add(fromCountry);
        card3.add(id);
        card3.add(tpass);
        card3.add(tname);
        card3.add(tsex);
        card3.add(tage);
        card3.add(tnationality);
        card3.add(tfromCountry);
        card3.add(tid);
        card3.add(bConfirm);
        
    }
    private void initCard4() {
        JTable roomTable = new JTable(roomDao.queryAllRoom(), roomTableHead);
        roomTable.setFont(font);
        roomTable.setPreferredSize(new Dimension(720, 440));
        roomTable.getTableHeader().setPreferredSize(new Dimension(0, 30));
        roomTable.getTableHeader().setFont(font1);
        roomTable.setRowHeight(30);
        roomTable.setEnabled(false); // 设置为不开操作
        JScrollPane scroll = new JScrollPane(roomTable);
        scroll.setPreferredSize(new Dimension(720, 430));
        
        card4.add(scroll);
    }
    private void initCard5() {
        // 业务办理（办理入住和退房）
       type.setBounds(150, 40, 80, 30);
       textType.setBounds(230, 40, 200, 30);
       bQueryR.setBounds(450, 40, 100, 30);
        rid.setBounds(150, 100, 80, 30);
        textRid.setBounds(230, 100, 200, 30);
        address.setBounds(150, 160, 80, 30);
        textAddress.setBounds(230, 160, 200, 30);
        count.setBounds(150, 220, 80, 30);
        textCount.setBounds(230, 220, 200, 30);
        price.setBounds(150, 280, 80, 30);
        textPrice.setBounds(230, 280, 200, 30);
        bIn.setBounds(180, 390, 90, 30);
        bReturn.setBounds(300, 390, 90, 30);
        
        type.setFont(font);
        textType.setFont(font);
        bQueryR.setFont(font);
        
        textRid.setEditable(false);
        textAddress.setEditable(false);
        textCount.setEditable(false);
        textPrice.setEditable(false);
        

        card5.add(type);
        card5.add(textType);
        card5.add(bQueryR);
        card5.add(bIn);
        card5.add(bReturn);
        card5.add(rid);
        card5.add(textRid);
        card5.add(address);
        card5.add(textAddress);
        card5.add(count);
        card5.add(textCount);
        card5.add(price);
        card5.add(textPrice);

        
    }
   
    private void initCard6(){
    	JTable recordTable=new JTable(recordDao.queryAllRecord(),recordTableHead);
    	recordTable.setFont(font);
    	recordTable.setPreferredSize(new Dimension(720,450));
    	recordTable.setEnabled(false);
    	
    	recordTable.getTableHeader().setPreferredSize(new Dimension(0,30));
    	recordTable.getTableHeader().setFont(font);
    	recordTable.setRowHeight(30);
    	
    	JScrollPane scroll=new JScrollPane(recordTable);
    	scroll.setPreferredSize(new Dimension(720,450));
    	
    	card6.add(scroll);
    }

	
	
	public static void adminShow(){
		JFrame a=new AdminUI();
		   a.setTitle("境外人员酒店隔离管理系统");
		   a.setVisible(true);
	       a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}

}
